package system

import (
	"fmt"
	"strconv"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SysPrescriptionRecordApi struct{}

// CreateSysPrescriptionRecord 创建药方
// @Tags SysPrescriptionRecord
// @Summary 创建药方
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescriptionRecord true "创建药方"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /sysPrescriptionRecord/createSysPrescriptionRecord [post]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) CreateSysPrescriptionRecord(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var sysPrescriptionRecord system.SysPrescriptionRecord
	err := c.ShouldBindJSON(&sysPrescriptionRecord)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前登录用户ID
	userID := utils.GetUserID(c)
	if userID == 0 {
		response.FailWithMessage("获取失败:用户ID不能为空", c)
		return
	}
	sysPrescriptionRecord.UserId = int64(userID)

	err = sysPrescriptionRecordService.CreateSysPrescriptionRecord(ctx, &sysPrescriptionRecord)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSysPrescriptionRecord 删除药方
// @Tags SysPrescriptionRecord
// @Summary 删除药方
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescriptionRecord true "删除药方"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /sysPrescriptionRecord/deleteSysPrescriptionRecord [delete]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) DeleteSysPrescriptionRecord(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := sysPrescriptionRecordService.DeleteSysPrescriptionRecord(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSysPrescriptionRecordByIds 批量删除药方
// @Tags SysPrescriptionRecord
// @Summary 批量删除药方
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /sysPrescriptionRecord/deleteSysPrescriptionRecordByIds [delete]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) DeleteSysPrescriptionRecordByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := sysPrescriptionRecordService.DeleteSysPrescriptionRecordByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSysPrescriptionRecord 更新药方
// @Tags SysPrescriptionRecord
// @Summary 更新药方
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescriptionRecord true "更新药方"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /sysPrescriptionRecord/updateSysPrescriptionRecord [put]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) UpdateSysPrescriptionRecord(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var sysPrescriptionRecord system.SysPrescriptionRecord
	err := c.ShouldBindJSON(&sysPrescriptionRecord)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = sysPrescriptionRecordService.UpdateSysPrescriptionRecord(ctx, sysPrescriptionRecord)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSysPrescriptionRecord 用id查询药方
// @Tags SysPrescriptionRecord
// @Summary 用id查询药方
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询药方"
// @Success 200 {object} response.Response{data=system.SysPrescriptionRecord,msg=string} "查询成功"
// @Router /sysPrescriptionRecord/findSysPrescriptionRecord [get]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) FindSysPrescriptionRecord(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	resysPrescriptionRecord, err := sysPrescriptionRecordService.GetSysPrescriptionRecord(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(resysPrescriptionRecord, c)
}

// GetSysPrescriptionRecordList 分页获取药方列表
// @Tags SysPrescriptionRecord
// @Summary 分页获取药方列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.SysPrescriptionRecordSearch true "分页获取药方列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /sysPrescriptionRecord/getSysPrescriptionRecordList [get]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) GetSysPrescriptionRecordList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.SysPrescriptionRecordSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := sysPrescriptionRecordService.GetSysPrescriptionRecordInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSysPrescriptionRecordPublic 不需要鉴权的药方接口
// @Tags SysPrescriptionRecord
// @Summary 不需要鉴权的药方接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /sysPrescriptionRecord/getSysPrescriptionRecordPublic [get]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) GetSysPrescriptionRecordPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	sysPrescriptionRecordService.GetSysPrescriptionRecordPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的药方接口信息",
	}, "获取成功", c)
}

// CheckStock 核准库存
// @Tags SysPrescriptionRecord
// @Summary 核准库存
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescriptionRecord true "核准库存"
// @Success 200 {object} response.Response{msg=string} "核准成功"
// @Router /sysPrescriptionRecord/checkStock [post]
func (sysPrescriptionRecordApi *SysPrescriptionRecordApi) CheckStock(c *gin.Context) {
	fmt.Println("核准库存")
	// 创建业务用Context
	ctx := c.Request.Context()
	query := c.Request.URL.Query()
	ID := query.Get("ID")
	IDInt, _ := strconv.Atoi(ID)

	fmt.Println("核准库存ID", IDInt)
	err := sysPrescriptionRecordService.CheckStock(ctx, int64(IDInt))
	if err != nil {
		global.GVA_LOG.Error("核准失败!", zap.Error(err))
		response.FailWithMessage("核准失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("核准成功", c)
}
